package cn.zhonghuanzhiyuan.sprc.basicinfo.service;

import cn.org.bachelor.iam.IamContext;
import cn.zhonghuanzhiyuan.sprc.common.utils.ReflectionUtil;
import cn.zhonghuanzhiyuan.sprc.common.utils.UuidUtil;


import java.util.Date;

/**
 * @author james
 * @createtime 2024/二月/22 23:57
 */


public interface CommonPropertyService {

    String  METHOD_SET_ID ="setId";
    String  METHOD_SET_ORG_ID ="setOrgId";
    String  METHOD_SET_PARK_ID ="setParkId";
    String  METHOD_SET_CREATE_USER ="setCreateUser";
    String  METHOD_SET_CREATE_TIME ="setCreateTime";
    String  METHOD_SET_UPDATE_USER ="setUpdateUser";
    String  METHOD_SET_UPDATE_TIME ="setUpdateTime";
    String  METHOD_SET_ADMINISTRATOR ="setAdministrator";
    /**
     * 设置新增时的公共属性
     *
     * @param target
     * @param iamContext
     */
    default void copyCreateProperties(Object target, IamContext iamContext) {

        if (ReflectionUtil.isExistMethod(target, METHOD_SET_ID, String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_ID, UuidUtil.getUUID());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_ORG_ID, String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_ORG_ID, iamContext.getUser().getOrgId());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_PARK_ID, String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_PARK_ID, iamContext.getUser().getTenantId());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_CREATE_USER ,String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_CREATE_USER ,iamContext.getUser().getName());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_CREATE_TIME, Date.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_CREATE_TIME, new Date());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_UPDATE_USER, String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_UPDATE_USER, iamContext.getUser().getName());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_UPDATE_TIME, Date.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_UPDATE_TIME, new Date());
        }


    }

    /**
     * 设置更新时的公共属性
     *
     * @param target
     * @param iamContext
     */
    default void copyUpdateProperties(Object target, IamContext iamContext) {

        if (ReflectionUtil.isExistMethod(target, METHOD_SET_UPDATE_USER, String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_UPDATE_USER, iamContext.getUser().getName());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_UPDATE_TIME, Date.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_UPDATE_TIME, new Date());
        }

    }

    /**
     * 设置查询时的公共属性
     *
     * @param target
     * @param iamContext
     */
    default void copyQueryProperties(Object target, IamContext iamContext) {

        if (ReflectionUtil.isExistMethod(target, METHOD_SET_ORG_ID, String.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_ORG_ID, iamContext.getUser().getOrgId());
        }
        if (ReflectionUtil.isExistMethod(target, METHOD_SET_ADMINISTRATOR, Boolean.class)) {
            ReflectionUtil.invokeMethod(target, METHOD_SET_ADMINISTRATOR, iamContext.getUser().isAdministrator());
        }

    }
}
